import 'package:flutter/material.dart';
import 'package:fluwx/fluwx.dart';
import './components/BottomNavigationBar.dart' as bottomNavs;
import 'dart:io';
import 'package:flutter/services.dart';

import 'package:sensors_analytics_flutter_plugin/sensors_analytics_flutter_plugin.dart';

// styles
import './assets/styles/global.dart' as styles;

// pages
import './pages/weShare.dart' as weShare;
import './pages/msgPush.dart' as msgPush;
import './pages/buryUp.dart' as buryUp;
import './pages/toMpWx.dart' as toMpWx;

void main() {
  if (Platform.isAndroid) {
    // 以下两行 设置android状态栏为透明的沉浸。写在组件渲染之后，是为了在渲染后进行set赋值，覆盖状态栏，写在渲染之前MaterialApp组件会覆盖掉这个值。
    SystemUiOverlayStyle systemUiOverlayStyle =
        SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MyAppState();
  }
}

class MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    _initFluwx();
  }

  int _currentPage = 2;

  final _widgetPageTitles = [
    Text('微信分享', style: styles.appBarTitleStyle),
    Text('消息推送', style: styles.appBarTitleStyle),
    Text('埋点上报', style: styles.appBarTitleStyle),
    Text('跳小程序', style: styles.appBarTitleStyle),
  ];
  final _widgetPages = [
    weShare.WeShare(),
    msgPush.MsgPush(),
    buryUp.BuryUp(),
    toMpWx.ToMpWx(),
  ];

  _initFluwx() async {
    await registerWxApi(
      appId: "wx59fd3792197c2906",
      doOnAndroid: true,
      doOnIOS: true,
      universalLink: "https://www.huanjutang.com/",
    );
    var result = await isWeChatInstalled;
    print("is installed $result");
  }

  void _handlePageChange(int index) {
    SensorsAnalyticsFlutterPlugin.track("Flutter_AddToFav", {
      "ProductID": 123456,
      "UserLevel": "VIP",
      "UserType": "onPressed",
    });
    this.setState(() {
      _currentPage = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'APP Demo',
      theme: ThemeData(primaryColor: Colors.blue),
      home: Scaffold(
        appBar: AppBar(
          title: Center(
            child: _widgetPageTitles[_currentPage],
          ),
          toolbarHeight: 50,
        ),
        body: Center(
          child: _widgetPages[_currentPage],
        ),
        // 底部导航条
        bottomNavigationBar:
            bottomNavs.bottomBars(_currentPage, _handlePageChange),
      ),
    );
  }
}
